package yxy.game.pm2.module.avatar;

import com.alibaba.fastjson.JSON;
import org.redisson.api.RSet;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.head;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Sign;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.BaseGameRedisLockBean;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.tip.TipHandle;
import yxy.game.pm2.module.tip.TipType;

import java.util.*;

//头像
public class HeadModule extends BaseGameRedisLockBean {

	static protected enum HeadCheckType {
		武将获取,;

		static protected HeadCheckType forNumber(int value) {
			switch (value) {
//			case 1:
//				return 武将星级;
			default:
				return 武将获取;
			}
		}
	}

	// bean===========================================================================

	// func==========================================================================

	static public void check(String playerCode, HeadCheckType type, int... params) {
		Map<String, head> datas = DataCenter.getAllData(head.class);
		Set<String> all = Sign.all(playerCode, CurrencyType.CT_头像);
		String lockKey = lockKey(playerCode);
		try {
			if (tryLock(lockKey)) {
				for (head data : datas.values()) {
					if (all.contains(data.getCode())) {
						continue;// 已经有这个形象了
					}
					HeadCheckType checkType = HeadCheckType.forNumber(data.getType());
					if (!checkType.equals(type)) {
						continue;
					}
					switch (checkType) {
					case 武将获取:
						if (params[0] != data.getCondition() / 100) {
							continue;
						}
						break;
					default:
						continue;
					}
					new CurrencyHandle().addCurrency(playerCode, CCReason.武将获取, Currency.create(CurrencyType.CT_头像, data.getId(), 1));
					add(playerCode, data.getCode());
				}
			}
		} catch (Exception e) {
		} finally {
			unlock(lockKey);
		}
	}

	static public Collection<Integer> newfound(String playerCode) {
		ArrayList<Integer> list = new ArrayList<>();

		String lockKey = lockKey(playerCode);
		try {
			if (tryLock(lockKey)) {
				Collection<String> all = touch(playerCode).readAll();
				for (String code : all) {
					list.add(Integer.parseInt(code));
				}
//				touch(playerCode).clear();
				new TipHandle().updateTip(playerCode, TipType.主公_头像切页, false);
			}
		} catch (Exception e) {
		} finally {
			unlock(lockKey);
		}

		return list;
	}

	// redis==========================================================================

	static public String key(String playerCode) {
		return String.format("PLAYER:%s:MODULE:HEAD", playerCode);
	}

	static private RSet<String> touch(String playerCode) throws Exception {
		RSet<String> module = redis().client().getSet(key(playerCode));
		if (!module.isExists()){
			String headStr = MySQLCacheDAO.getV(key(playerCode), String.class);
			ArrayList<String> headList = new ArrayList<>(Arrays.asList(headStr.split(",")));

			for(int i = 0; i < headList.size();i++){
				module.add(headList.get(i));
			}
		}
		return module;
	}

	static private void add(String playerCode, String headCode) {
		try {
			touch(playerCode).add(headCode);
			new TipHandle().updateTip(playerCode, TipType.主公_头像切页, true);
		} catch (Exception e) {
		}
	}

	//存储到数据库
	public static void saveToDB(String playerCode) {
		try {
			String key = key(playerCode);
			RSet<String> module = redis().client().getSet(key(playerCode));
			if (module.isExists()) {
				String value = "";
				for (String str:module){
					value = (!value.equals("") && !str.equals("")) ? value + "," + str : str;
				}

				if(module !=null) MySQLCacheDAO.setV(key, value);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			touchBucket(key(playerCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}
	// lock==========================================================================

	static private String lockKey(String playerCode) {
		return String.format("lock:%s", key(playerCode));
	}

}
